package com.jucai.pppeiyouclient.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.QuickFragmentAdapter;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.ScreenUtil;
import com.jucai.pppeiyouclient.view.fragment.HistoricalActivitiesFragment;
import com.jucai.pppeiyouclient.view.fragment.RunningActivitiesFragment;

import java.util.ArrayList;
import java.util.List;

public class MyActivitiesActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private TextView titleText;
    private ViewPager activitiesVp;
    private TabLayout activitiesTab;
    private List<Fragment> fragments = new ArrayList<>();
    private QuickFragmentAdapter<Fragment> fragmentAdapter;
    private String[] titles = {"当前活动", "历史活动"};
    private int selectPos = 0;
    private LinearLayout linearLayout;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_activities);
        fragments.add(new RunningActivitiesFragment());
        fragments.add(new HistoricalActivitiesFragment());
        titleText = (TextView) findViewById(R.id.common_title_text);
        fragmentAdapter = new QuickFragmentAdapter<>(getSupportFragmentManager(), fragments, titles);
        activitiesVp = (ViewPager) findViewById(R.id.activities_vp);
        activitiesTab = (TabLayout) findViewById(R.id.activities_tab);
        activitiesVp.setAdapter(fragmentAdapter);
        activitiesTab.setupWithViewPager(activitiesVp);
        activitiesVp.setOffscreenPageLimit(2);

        linearLayout = (LinearLayout) activitiesTab.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.shape_vertical_divider_gray));
        linearLayout.setDividerPadding(ScreenUtil.dip2px(MyActivitiesActivity.this, 15));
    }

    @Override
    protected void initData() {

        selectPos = getIntent().getIntExtra("position", 0);
        switch (selectPos) {
            case 0:
                activitiesVp.setCurrentItem(0);
                titleText.setText("当前活动");
                break;
            case 1:
                activitiesVp.setCurrentItem(1);
                titleText.setText("历史活动");
                break;
            default:
                break;
        }
    }

    @Override
    protected void loadNetData() {

    }

    public static void startActivities(Context context, int position) {
        Intent intent = new Intent(context, MyActivitiesActivity.class);
        intent.putExtra("position", position);
        context.startActivity(intent);
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        activitiesVp.addOnPageChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;
            case R.id.common_home_back:
                startActivity(new Intent(MyActivitiesActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            titleText.setText("当前活动");
        } else if (position == 1) {
            titleText.setText("历史活动");
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
